# encoding: utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
__author__ = 'wwj'
import MySQLdb
import MySQLdb.cursors
from datetime import *
sys.path.append("/Users/wwj/Downloads/shangtianjun/girl/")
from girl.dbShell.dbshell import dbshell
# from ..dbshell import dbshell
class main(dbshell):
    orderItem = [
        ['gid', ''],
        ['sid', ''],
        ['title', ''],
        ['thumb', ''],
        ['url', ''],
    ]
    existgirl = {}
    mongoTable = "diary"
    mysqltable = "diary"
    def __init__(self, spidername):
        super(main, self).__init__(spidername)
        self.geturltool("select distinct(url) from diary")
        # self.mongofrom = self.mongo.find({"date": "2018-09-06"})
        # import ipdb;ipdb.set_trace()
        self.getgirls()

    def getgirls(self):
        # tmp = self.client[self.config.get('mongoDb')]["exitgirl"].find({}, {"_id": 0, "url": 1})
        # if tmp.count() == 0:
            # tmp = self.getdb("select url,id,sid from girl")
            self.client[self.config.get('mongoDb')]["exitgirl"].insert_many(
                [dict(url={i.split()[0]: (i.split()[1], i.split()[2])}) for i in tmp])
            tmp = self.client[self.config.get('mongoDb')]["exitgirl"].find({}, {"url": 1, "_id": 0})
        # for x in tmp:
        #     self.existgirl.update(x["url"])
        tmp = self.getdb("select url,id,sid from girl")
        self.redisconn.has


    def clean(self, y):
        data = self.data
        if not y.get("girlUrl"):
            raise self.myException("no girlurl")
        url = y["girlUrl"].replace("https://www.cityheaven.net", "")
        if not self.existgirl.get(url):
            raise self.myException("no girlexist")
        data["gid"],data["sid"] = self.existgirl.get(url)
        data["title"] = MySQLdb.escape_string(y["title"])
        data["thumb"] = y.get("image_urls")
        data["url"] = y["url"].replace("https://www.cityheaven.net", "")
        print data


if __name__ == '__main__':
    obj = main("city")
    obj()
